package com.CST.controller.cb;


import cn.hutool.core.date.DateTime;
import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.dto.cb.BdEnumdetailDTO;
import com.CST.entity.cb.BdDepartment;
import com.CST.entity.cb.BdEnumFuzzy;
import com.CST.entity.cb.BdEnumdetail;
import com.CST.logger.LogRecord;
import com.CST.service.cb.IBdDepartmentService;
import com.CST.service.cb.IBdEnumdetailService;
import com.CST.util.PageHelperTool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ly
 * @since 2021-09-23
 */
@Api(tags = {"字典数据操作"})
@RestController
@RequestMapping("/bdEnumdetail")
public class BdEnumdetailController {

    @Autowired
    private IBdEnumdetailService bdEnumdetailService;


    @Autowired
    private IBdDepartmentService iBdDepartmentService;

    //获得总的COST查询 不加edparentcode
    @PostMapping("/getEnumDetailSource")
    @ApiOperation("获取字典数据,必传参数:edParentCode:选择对应枚举")
    public ResultObject getEnumDetailSource(@RequestBody BdEnumdetail entity) {
        if (entity.getEdParentCode().isEmpty()) {
            return ResultObject.errorReturn("枚举edParentCode值不能为空");
        }
        return ResultObject.successReturn(bdEnumdetailService.getEnumDetailSource(entity));
    }

    @PostMapping("/getEnumDetailSourcePage")
    @ApiOperation("获取字典数据,必传参数:edParentCode:选择对应枚举")
    public ResultObject getEnumDetailSourcePage(@RequestBody BdEnumdetail entity) {
        if (entity.getEdParentCode().isEmpty()) {
            return ResultObject.errorReturn("枚举edParentCode值不能为空");
        }
        List<BdEnumdetail> result = bdEnumdetailService.getEnumDetailSource(entity);
        return ResultObject.successReturn(
                PageHelperTool.excutePageInfo(result, entity.getPageIndex(), entity.getPageSize()));
    }

    @PostMapping("/listEnumDetail")
    @ApiOperation("获取字典数据,必传参数:edParentCode:选择对应枚举 不带分页")
    public ResultObject listEnumDetail(@RequestBody BdEnumdetail bdEnumdetail) {
        return bdEnumdetailService.listEnumDetail(bdEnumdetail);
    }

    @PostMapping("/listEdFirstByFuzzy")
    @ApiOperation("获取对应关系edFist字段，做去重处理，根据edParentCode确定")
    public ResultObject listEdFirstByFuzzy(@RequestBody BdEnumdetail bdEnumdetail) {
        List<BdEnumdetail> bdEnumdetailList = bdEnumdetailService.listEdFirstByFuzzy(bdEnumdetail);
        return ResultObject.successReturn(bdEnumdetailList);
    }

//    @PostMapping("/updateHisStatusByEdId")
//    @ApiOperation("更新 科室-his科室 的临时启用状态 需要edId")
//    public Response updateHisStatusByEdId(@RequestBody BdEnumdetail entity){
//        return targetService.updateHisStatusByEdId(entity);
//    }

    /**
     * 插入数据到 科室—his科室
     *
     * @return
     */
//    @PostMapping("/insertHis")
//    @ApiOperation("插入数据到 科室his-科室的临时启用状态 需要edId和status")
//    public Response insertEnumDetailWithHis(@RequestBody ){
//        
//    }

    //获得薪酬,工资,HIS 换一个edParentCode的枚举方法
    @PostMapping("/getLikeEnumDetailSource")
    @ApiOperation("获取字典数据,必传参数:edParentCode:选择对应枚举")
    public ResultObject getLikeEnumDetailSource(@RequestBody BdEnumdetail entity) {
        if (entity.getEdParentCode().isEmpty()) {
            return ResultObject.errorReturn("枚举edParentCode值不能为空");
        }
        return ResultObject.successReturn(bdEnumdetailService.getLikeEnumDetailSource(entity));
    }


    @PostMapping("/addEnumDetail")
    @ApiOperation("添加字典数据,必传参数:edEDEdId:传'1',edName:传值,edParentCode:选择对应枚举")
    public Object addEnumDetail(@RequestBody BdEnumdetailDTO entity) {

        List<String> edExtraSecondCode = entity.getEdExtraSecondCode();
        String edExtraFirstCode = entity.getEdExtraFirstCode();
    if (edExtraSecondCode.size()>0) {
        for (String deptCode : edExtraSecondCode) {
            BdEnumdetail bdEnumdetail = new BdEnumdetail();
            BeanUtils.copyProperties(entity, bdEnumdetail, null, "edId");
            bdEnumdetail.setEdCode(edExtraFirstCode + "-" + deptCode);
            bdEnumdetail.setEdExtraSecondCode(deptCode);
            LambdaQueryWrapper<BdDepartment> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdDepartment::getDeptCode, deptCode);
            BdDepartment bdDepartment = iBdDepartmentService.getOne(queryWrapper);
            bdEnumdetail.setEdExtraSecondDescription(bdDepartment.getDeptName());

            bdEnumdetail.setCreatedOn(DateTime.now().toString());
            bdEnumdetail.setUpdatedOn(DateTime.now().toString());

            if (bdEnumdetail.getEdEdId().equals(0) || bdEnumdetail.getEdName().isEmpty() || bdEnumdetail.getEdParentCode().isEmpty()) {
                return ResultObject.errorReturn("枚举edParentCode值/院区edEdId/名称edName不能为空");
            }
            boolean flag = bdEnumdetailService.save(bdEnumdetail);
            if (!flag) {
                return new Response(500, "添加失败");

            }
//         Response result = bdEnumdetailService.addEnumDetail(bdEnumdetail);
//           // return ResultObject.resultReturn(result);
        }
    }else {
        BdEnumdetail bdEnumdetail = new BdEnumdetail();
        BeanUtils.copyProperties(entity, bdEnumdetail, null, "edId");
        bdEnumdetail.setCreatedOn(DateTime.now().toString());
        bdEnumdetail.setUpdatedOn(DateTime.now().toString());
        if (bdEnumdetail.getEdEdId().equals(0) || bdEnumdetail.getEdName().isEmpty() || bdEnumdetail.getEdParentCode().isEmpty()) {
            return ResultObject.errorReturn("枚举edParentCode值/院区edEdId/名称edName不能为空");
        }
        boolean flag = bdEnumdetailService.save(bdEnumdetail);
        if (!flag) {
            return new Response(500, "添加失败");
        }
    }
        return new Response(200,"添加成功");

    }

    @PostMapping("/modifyEnumDetail")
    @ApiOperation("修改字典数据 必传参数:edId:选择修改记录edId")
    public ResultObject modifyEnumDetail(@RequestBody BdEnumdetail entity) {
        if (entity.getEdParentCode().isEmpty()) {
            return ResultObject.errorReturn("主键edParentCode值不能为空");
        }
        Response result = bdEnumdetailService.modifyEnumDetail(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/batchDeleteEnumDetail")
    @ApiOperation("删除对应关系, 传id多个以逗号隔开")
    public ResultObject batchDeleteEnumDetail(@RequestBody List<Integer> ids) {
        if (ids == null || ids.stream().count() == 0) {
            return ResultObject.errorReturn("请选择要删除的记录");
        }
        Response result = bdEnumdetailService.batchDeleteEnumDetail(ids);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/listEnumDetailIsLinkByFuzzy")
    @ApiOperation("根据条件查询科室对应关系的关联状态查询")
    public ResultObject listEnumDetailIsLink(@RequestBody BdEnumFuzzy entity) {
        return bdEnumdetailService.listEnumDetailIsLink(entity);
    }

    @GetMapping("/download/DeptSalaryRelation")
    public void downloadExcelDeptSalaryRelation(HttpServletResponse response) throws ClassNotFoundException {
        bdEnumdetailService.exportExcelDeptSalaryRelation(response);
    }

    @GetMapping("/download/DeptHisRelation")
    public void downloadExcelDeptHisRelation(HttpServletResponse response) throws ClassNotFoundException {
        bdEnumdetailService.exportExcelDeptHisRelation(response);
    }

    @GetMapping("/download/DeptUFIDARelation")
    public void downloadExcelDeptUFIDARelation(HttpServletResponse response) throws ClassNotFoundException {
        bdEnumdetailService.exportExcelDeptUFIDARelation(response);
    }
    @PostMapping("/listEnumDetailUnLink")
    @ApiOperation("根据sysId和edParentCode查询未 科室对应 的未关联数据")
    public ResultObject listEnumDetailUnLink(@RequestBody BdEnumFuzzy entity) {
        return bdEnumdetailService.listEnumDetailUnLink(entity);
    }

//    
//    @PostMapping("/listEnumDetailIsLinkByHIS")
//    @ApiOperation("查询HIS的对应和未对应关系")
//    public ResultObject listEnumDetailIsLinkByHIS(@RequestBody BdEnumdetail entity) {
//        return targetService.listEnumDetailIsLinkByHIS(entity);
//    }

    /**
     * 更新科室状态 启用与未启用
     * @return
     */
//    @PostMapping("/updateRowStatus")
//    @ApiOperation("更新工资对应页面中的启用状态 根据EDID")
//    public Response updateRowStatus(@RequestBody BdEnumdetail entity){
//        return targetService.updateRowStatus(entity);
//    }

    /**
     * 新增科室工资对应表
     *
     * @return
     */
//    @PostMapping("/insertEnumDetailWithSalary")
//    @ApiOperation("插入科室工资对应数据 需要edExtraFirstCode,edExtraFirstDescription,edExtraSecondCode,edExtraSecondDescription")
//    public Response insertEnumDetailWithSalary(@RequestBody BdEnumdetail entity){
//        targetService.setE
//        return targetService.insertEnumDetailWithSalary(entity);
//    }

//    @PostMapping("/getEnumDetailWithSalary/{edId}")
//    @ApiOperation("查询科室的信息 需要edId 返回两列Name")
//    public ResultObject getEnumDetailWithSalary(@PathVariable("edId") String edId){
//        return targetService.getEnumDetailWithSalary(edId);
//    }

//    @PostMapping("/updateEnumDetailWithSalaryInfo")
//    @ApiOperation("根据edId修改科室的基本信息")
//    public Response updateEnumDetailWithSalaryInfo(@RequestBody BdEnumBaseInfo entity){
//        return targetService.updateEnumDetailWithSalaryInfo(entity);
//    }
    @PostMapping("/setDefault")
    @ApiOperation("设置默认规则 必传参数:edParentCode:BD_Organization,edId:选择修改记录edId")
    public ResultObject setDefault(@RequestBody BdEnumdetail entity) {
        if (entity.getEdId() == 0 || entity.getEdParentCode().isEmpty()) {
            return ResultObject.successReturn(Response.error("主键/枚举edParentCode不能为空"));
        }
        try {
            bdEnumdetailService.setDefault(entity);
        } catch (Exception ex) {
            return ResultObject.successReturn(Response.error(String.join(":", "设置失败", ex.getMessage())));
        }
        return ResultObject.successReturn(Response.success("设置成功"));

    }

    @LogRecord(module = "字典数据")
    @PostMapping("/test")
    @ApiOperation("测试")
    public ResultObject test(@RequestBody BdEnumdetail entity) throws Exception {

        throw new Exception("cwwfewf");

    }

}

